// ++++++++++++++++++++++++++++++++++++++++
// 《零基础Go语言算法实战》源码
// ++++++++++++++++++++++++++++++++++++++++
// Author:廖显东（ShirDon）
// Blog:https://www.shirdon.com/
// Gitee:https://gitee.com/shirdonl/goAlgorithms.git
// Buy link :https://item.jd.com/14101229.html
// ++++++++++++++++++++++++++++++++++++++++

package main

import (
	"fmt"
	"math"
)

// 点结构体
type Point struct {
	X, Y, Z float64
}

// 三角形结构体
type Triangle struct {
	A, B, C Point
}

// 计算周长
func (t *Triangle) Perimeter() float64 {
	ab := math.Sqrt(math.Pow(t.B.X-t.A.X, 2) + math.Pow(t.B.Y-t.A.Y, 2) + math.Pow(t.B.Z-t.A.Z, 2))
	bc := math.Sqrt(math.Pow(t.C.X-t.B.X, 2) + math.Pow(t.C.Y-t.B.Y, 2) + math.Pow(t.C.Z-t.B.Z, 2))
	ca := math.Sqrt(math.Pow(t.A.X-t.C.X, 2) + math.Pow(t.A.Y-t.C.Y, 2) + math.Pow(t.A.Z-t.C.Z, 2))
	return ab + bc + ca
}

// 计算面积
func (t *Triangle) Area() float64 {
	ab := math.Sqrt(math.Pow(t.B.X-t.A.X, 2) + math.Pow(t.B.Y-t.A.Y, 2) + math.Pow(t.B.Z-t.A.Z, 2))
	bc := math.Sqrt(math.Pow(t.C.X-t.B.X, 2) + math.Pow(t.C.Y-t.B.Y, 2) + math.Pow(t.C.Z-t.B.Z, 2))
	ca := math.Sqrt(math.Pow(t.A.X-t.C.X, 2) + math.Pow(t.A.Y-t.C.Y, 2) + math.Pow(t.A.Z-t.C.Z, 2))
	s := (ab + bc + ca) / 2
	return math.Sqrt(s * (s - ab) * (s - bc) * (s - ca))
}

func main() {
	t := Triangle{
		A: Point{1, 1, 1},
		B: Point{2, 2, 2},
		C: Point{1, 6, 8},
	}
	fmt.Printf("周长: %.2f\n", t.Perimeter())
	fmt.Printf("面积: %.2f\n", t.Area())
}

//$ go run 3DTriangle.go
//周长: 17.61
//面积: 4.42
